package com.medicine.hbp.ui;


import java.util.Set;

import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import com.medicine.hbp.R;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.http.LoginAppHttpCallBack;
import com.medicine.hbp.mob.LoginUserVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.BaiduApi;
import com.medicine.hbp.uitl.CommUtil;
import com.medicine.hbp.uitl.MD5;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.uitl.BaiduApi.OnBDLocationListener;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * 登录界面
 *
 * @author Administrator
 */
public class LoginActivity extends BaseActivity {
	@BindView(id = R.id.text_title, click = false)
	public TextView text_title;
	@BindView(id = R.id.btn_image_left_exit, click = true)
	public ImageView btn_image_left_exit;

	@BindView(id = R.id.edt_login_name, click = false)
	public EditText editName;
	@BindView(id = R.id.edit_password, click = false)
	public EditText editPassword;
	@BindView(id = R.id.btn_login, click = true)
	public Button btnLogin;
	@BindView(id = R.id.btn_register, click = true)
	public Button btn_register;
	@BindView(id = R.id.btn_password, click = true)
	public Button btn_password;
	//	private DBManager manager;
	private BaiduApi api;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_login);
	}
	@Override
	public void initWidget() {
		super.initWidget();
//		editName.setText("18608049923");
//		editPassword.setText("123456");
		btn_image_left_exit.setVisibility(View.VISIBLE);
		text_title.setText("登录");
		api=new BaiduApi(aty,new OnBDLocationListener() {

			@Override
			public void onYes(String  pro, String city) {
				tpro=pro;
				tcity=city;
			}
			@Override
			public void onNo() {
			}
		});
		api.StartBaidu();
//		manager = new DBManager(aty);
		//
		// editName.setText("1234567890");
		// editPassword.setText("123456789");
	}
	private String tpro,tcity;
	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
		switch (v.getId()) {
			case R.id.btn_login:// 登陆
				String name = editName.getText().toString();
				String password = editPassword.getText().toString();
				if (TextUtils.isEmpty(name)) {
					ToastUtil.showShort(aty, "请输入您的账户");
					return;
				}
//			Matcher matcherMailbox = PatternUtils.patternNumber.matcher(name);
//			if (!matcherMailbox.matches()) {
//				ToastUtil.showShort(aty, "请输入正确的手机号");
//				return;
//			}
				if (TextUtils.isEmpty(password)) {
					ToastUtil.showShort(aty, "请输入您的账户密码");
					return;
				}
//			if(!TextUtils.isEmpty(MyApplication.getMyApplication().getCookitString())){
//				httpLogout(name, password);
//			}else{
				httpLoginUser(name, password);
//			}
				break;
			case R.id.btn_register:// 注册
				Intent intent1 = new Intent(aty, RegisterActivity.class);
				startActivityForResult(intent1, 1000);
				break;
			case R.id.btn_password:// 注册
				Intent intent2 = new Intent(aty, PasswordBackActivity.class);
				startActivity(intent2);
				break;
			case R.id.btn_image_left_exit://退出
				CommUtil.destroySelfUserTwoActivity();
				finish();
				break;
			default:
				break;
		}
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			Intent intent = new Intent();
			intent.putExtra("isTab", true);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
//	/**
//	 * 退出登录
//	 */
//	private void httpLogout(final String name, final String psd) {
//		HttpParams params = new HttpParams();
//		AppHttps.HttpPost(HttpInterface.LOGIN_OUT, params,
//				new AppHttpCallBack<Object>(Object.class, aty) {
//					@Override
//					public void onSuccess(Object result) {
////						if(manager.query().size()>0){
////							manager.deleteOldPerson(manager.query().get(0));
////						}
//						MyApplication.getMyApplication().setCookitString("");
//						httpLoginUser(name, psd);
//					}
//
//					@Override
//					public void onFailures(int errorCode, String errorMsg) {
//						ToastUtil.showShort(aty, errorMsg);
//					}
//				});
//
//	}
	/**
	 * 登陆接口
	 */
	private void httpLoginUser(String name, String psd) {
		HttpParams params = new HttpParams();
		params.put("account", name);// 用户账户(测试接口正确为user)
		params.put("password", MD5.getMD5String(MD5.getMD5String(psd)));// 用户密码(测试接口正确为pass)
//		ToastUtil.showShort(aty, name+"---"+psd);
		AppHttps.HttpPost(this,HttpInterface.LOGIN_USER, params,
				new LoginAppHttpCallBack<LoginUserVo>(LoginUserVo.class, aty) {
					@Override
					public void onSuccess(LoginUserVo result) {
						if (result.userInfo != null) {
							LoginSp.setUser(result.userInfo, aty);
							Intent intent = new Intent();
							intent.putExtra("isTab", true);
							setResult(RESULT_OK, intent);
							ToastUtil.showShort(aty, "登录成功");
							JPushInterface.setAliasAndTags(aty, "com.medicine.hbp"+LoginSp.getUserId(aty), null, new TagAliasCallback() {
								@Override
								public void gotResult(int arg0, String arg1, Set<String> arg2) {
									if(arg0==0){
									}
								}
							});
							if(TextUtils.isEmpty(tpro)){
								finish();
							}else{
								httpUpdateCity(tpro,tcity);
							}
						}
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}
	private void httpUpdateCity(String pro,String city) {
		HttpParams params = new HttpParams();
		params.put("userToken", CommUtil.getIMEI(aty));
		params.put("pro", pro);
		params.put("city", city);
		AppHttps.HttpPost(this, HttpInterface.UPDATE_CITY, params,
				new AppHttpCallBack<Object>(Object.class,aty) {
					@Override
					public void onSuccess(Object result) {
						finish();
					}
					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});
	}
	@Override
	protected void onDestroy() {
		Intent intent = new Intent();
		intent.putExtra("isTab", false);
		setResult(RESULT_OK, intent);
		super.onDestroy();
	}
}
